/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import healthcare.coursework.model.Patient;
import healthcare.coursework.service.PatientService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zkplus.databind.BindingListModelList;

import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Window;

/**
 *
 * @author Administrator
 */
public class TrackPatient extends GenericAutowireComposer {

    //Componennt
    private Combobox nameSearch;
    private Button patientQuery;
    private Listbox patientDataListView;
    //Service
    private PatientService patientService = util.Utility.getUtility().getPatientService();
    private Paging patientPaging;
    private List<Patient> patients;

    public TrackPatient() {
        System.out.println("Teaegafg");
    }

    private void setInfo() {
        doFilter();
        nameSearch.setModel(new ListModelList(patients, true));
    }

    public void onPatientQuery(Event event) {
        doFilter();
    }

    public void onLoadList(Event event) {
        System.out.println("Tesst");
        setInfo();
    }

    public void onViewPatient(Event event) {
        try {
            Map params = new HashMap();
            params.put("patient", patientService.findPatient((Long) patientDataListView.getSelectedItem().getValue()));
            System.out.println("View: " + ((Patient) params.get("patient")).getPatientId());
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/doctor/ViewPatientProfile.zul", null, params);

            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public void onAddPatient(Event event) {
        try {
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/adminStaff/AddPatient.zul", null, null);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void onListOfAppointment(Event event) {
        try {
            Long patientId = (Long) patientDataListView.getSelectedItem().getValue();
            Executions.sendRedirect("/adminStaff/ViewAppointments.do?patientId=" + patientId);
        } catch (Exception e) {
            try {
                Messagebox.show("Please select one patient to display appointments?", "Question", Messagebox.OK , Messagebox.INFORMATION);
            } catch (InterruptedException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public void onEditPatient(Event event) {
        try {
            Map params = new HashMap();
            params.put("patient", patientService.findPatient((Long) patientDataListView.getSelectedItem().getValue()));
            System.out.println("Track sample of patient: " + ((Patient) params.get("patient")).getPatientId());
            Window win = (Window) Executions.createComponents(
                    "/WEB-INF/zul/pages/adminStaff/EditPatient.zul", null, params);
            win.setMaximizable(true);
            win.setClosable(true);
            win.doModal();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void onTrackSample(Event event) {
        Long patientId = (Long) patientDataListView.getSelectedItem().getValue();
        Executions.sendRedirect("/TrackSamples.do?patientId=" + patientId);
    }

    public void onDeletePatient(Event event) {

        try {
            int status = Messagebox.show("Are you sure to remove patients?", "Question", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (((Integer) event.getData()).intValue() == Messagebox.YES) {
                        System.out.println("Enter remove method");
                        Iterator it = patientDataListView.getSelectedItems().iterator();
                        System.out.println(patientDataListView.getSelectedItems().size());
                        while (it.hasNext()) {
                            Listitem li = (Listitem) it.next();
                            Long patientId = (Long) li.getValue();
                            patientService.removePatient(patientId);
                            System.out.println("Patient with Id " + patientId + " has been deleted");
                        }
                        doFilter();
                        return;
                    } else {
                        System.out.println("Messagebox.No selected!");
                        return;
                    }
                }
            });



        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private void doFilter() {
        try {
            if (nameSearch.getText() == null || nameSearch.getText().trim().equals("")) {
                patients = patientService.findPatients();


            } else {
                patients = patientService.findPatientsByFullName(nameSearch.getValue());


            }
        } catch (Exception e) {
            patients = patientService.findPatients();


        }

        patientDataListView.setModel(new BindingListModelList(patients, true));

    }
}
